﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace PeopleEntity
{
    public class WeightAttribute : ValidationAttribute, IClientValidatable, IMetadataAware
    {
        public int MaxWeight { get; set; }

        public override bool IsValid(object value)
        {
            return value != null && (float) value < MaxWeight && (float)value>0;
        }
        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule
            {
                ErrorMessage = ErrorMessage,
                ValidationType = "weightvalidation"
            };
            rule.ValidationParameters["maxweight"] = MaxWeight;
            yield return rule;
        }

        public void OnMetadataCreated(ModelMetadata metadata)
        {
            metadata.DataTypeName = "Float";
        }
    }
}
